package pe.com.cineplanet.dao.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import pe.com.cineplanet.bean.cineplanet.Cine;
import pe.com.cineplanet.dao.dao.CineDAO;


public class CineDAOImpl implements CineDAO{

	protected JdbcTemplate jdbcTemplate;

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	
	public Cine findById(String cineId) {

		String sql = "select id, nombre, direccion from T_CINE where id=?";
		Object[] params = new Object[]{ cineId };
		
		try{
			return (Cine) jdbcTemplate.queryForObject(sql, params, new RowMapper(){

				public Cine mapRow(ResultSet rs, int arg1) throws SQLException {
					
					return new Cine(rs.getString(1), rs.getString(2));
					
				}
				
			});
		}catch(EmptyResultDataAccessException e){
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Cine> list() {
		String sql = "select id, nombre, direccion from T_CINE";
		
		try{
			return jdbcTemplate.query(sql, new RowMapper(){

				public Cine mapRow(ResultSet rs, int arg1) throws SQLException {
					
					return new Cine(rs.getString(1), rs.getString(2));
					
				}
				
			});
		}catch(EmptyResultDataAccessException e){
			return null;
		}
	}

}

